package com.meetingshow.common.base.uiservice;

import java.util.Set;

import javax.validation.Configuration;
import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.ValidatorFactory;

import com.meetingshow.common.base.uiservice.exception.ValidaterException;
import com.meetingshow.common.validator.MeetingShowTraversableResolver;

public abstract class BaseUIServiceImp {
    
    /**
     * 抛出数据验证异常
     * 
     * @throws ValidaterException
     *             数据验证异常
     */
    protected void throwValidaterException() throws ValidaterException {
        throw new ValidaterException();
    }

    /**
     * 抛出数据验证异常
     * 
     * @param message
     *            异常说明
     * @throws ValidaterException
     *             数据验证异常
     */
    protected void throwValidaterException(String message)
            throws ValidaterException {
        throw new ValidaterException(message);
    }
    
    /**
     * 通用验证方法，对Hibernate Validator进行了封装
     * 
     * @param <T>
     *            返回值泛型
     * @param arg0
     *            被验证的对象
     * @param arg1
     *            验证规则的group
     * @return Set<ConstraintViolation<T>> 验证过程所有约束的冲突集合
     */
    protected <T> Set<ConstraintViolation<T>> validate(T arg0, Class<?>... arg1) {
        Configuration<?> configuration = Validation.byDefaultProvider()
                .configure();
        ValidatorFactory factory = configuration.traversableResolver(
                new MeetingShowTraversableResolver()).buildValidatorFactory();

        return factory.getValidator().validate(arg0, arg1);
    }

    /**
     * 通用验证方法，对Hibernate Validator进行了封装
     * 
     * @param <T>
     *            返回值泛型
     * @param arg0
     *            被验证的对象
     * @param arg1
     *            验证规则的group
     * @return String 验证失败的文字描述
     */
    protected <T> String validateMsg(T arg0, Class<?>... arg1) {
        Configuration<?> configuration = Validation.byDefaultProvider()
                .configure();
        ValidatorFactory factory = configuration.traversableResolver(
                new MeetingShowTraversableResolver()).buildValidatorFactory();

        StringBuilder sBuilder = new StringBuilder();
        Set<ConstraintViolation<T>> cvs = factory.getValidator().validate(arg0,
                arg1);

        for (ConstraintViolation<T> cv : cvs) {
            sBuilder.append(cv.getPropertyPath()).append(" : ").append(
                    cv.getMessage()).append(";");
        }

        return sBuilder.toString();
    }

    /**
     * 通用验证方法，对Hibernate Validator进行了封装
     * 
     * @param <T>
     *            返回值泛型
     * @param arg0
     *            被验证的对象
     * @param arg1
     *            验证规则的group
     * @throws ValidaterException
     *             验证不通过时直接抛出异常，并将验证失败的文字描述放在异常信息中
     */
    protected <T> void validateThrowEx(T arg0, Class<?>... arg1)
            throws ValidaterException {
        Configuration<?> configuration = Validation.byDefaultProvider()
                .configure();
        ValidatorFactory factory = configuration.traversableResolver(
                new MeetingShowTraversableResolver()).buildValidatorFactory();

        StringBuilder sBuilder = new StringBuilder();
        Set<ConstraintViolation<T>> cvs = factory.getValidator().validate(arg0,
                        arg1);

        for (ConstraintViolation<T> cv : cvs) {
            sBuilder.append(cv.getPropertyPath()).append(" : ").append(
                    cv.getMessage()).append(";");
        }

        if (sBuilder.length() > 0) {
            throwValidaterException(sBuilder.toString());
        }
    }
}
